#!/bin/bash

#************************ Variables ************************ 
COLOR_WINTER="#ffffff"
COLOR_SPRING="#00ab00"
COLOR_SUMMER="#e7ce00"
COLOR_AUTUMN="#ff661b"
COLOR_DSKY="#061758"
COLOR_LSKY="#6084e2"
cyear=$(date +%Y)
NEW_YEARS_EVE_LYEAR=$(date +%j -d "12/31/$((cyear-1))")
NEW_YEARS_EVE=$(date +%j -d "12/31/$cyear")
SOLSTICE_WINTER_LYEAR=$(date +%j -d "12/21/$((cyear-1))")
SOLSTICE_WINTER=$(date +%j -d "12/21/$cyear")
EQUINOX_SPRING_NYEAR=$(date +%j -d "03/21/$((cyear+1))")
EQUINOX_SPRING=$(date +%j -d "03/21/$cyear")
let EQUINOX_SPRING="10#$EQUINOX_SPRING"
SOLSTICE_SUMMER=$(date +%j -d "06/21/$cyear")
EQUINOX_AUTUMN=$(date +%j -d "09/22/$cyear")
today=$(date +%j)
let today="10#$today"
tmp_gradient="seasons_gradient.png"
wallpaper_tmp="todays_wallpapux_tmp.png"
wallpaper="todays_wallpapux.png"
pp_date="30"
pp_hdate="15" 
psize_date="50"
ref_psize="1360"
tmp_text="tmp_text.png"
var_mandatory=( "width" "height" )

#************************ Functions ************************  
initialize () {
	while read line; do
		eval "$line"
		if [[ $? -ne 0 ]]; then
			echo "Error : $line is not correct!" >&2
			exit 1
		fi
	done < "config"
	
	for var in ${var_mandatory[@]}; do
		if [[ -z ${!var} ]]; then
			echo "Error : $var is not define!" >&2
			exit 1
		fi
	done
}

compute_color () {
	cday=$4
	todays_color=$(convert xc:$1 xc:$2 +append -filter Cubic -resize ${3}x1! -crop 1x1+$((cday-1))+0 txt:- | sed -n 's/.* \(#.*\)/\1/p' |awk '{ print $1 }')
}

compute_todays_color () {
	if [ $today -lt $EQUINOX_SPRING ]; then # In the winter
		total_days=$((NEW_YEARS_EVE_LYEAR - SOLSTICE_WINTER_LYEAR + EQUINOX_SPRING))
		cday=$((NEW_YEARS_EVE_LYEAR - SOLSTICE_WINTER_LYEAR + today))
		compute_color $COLOR_WINTER $COLOR_SPRING $total_days $cday
	elif [ $today -lt $SOLSTICE_SUMMER ]; then # In the spring
		cday=$((today - EQUINOX_SPRING))
		compute_color $COLOR_SPRING $COLOR_SUMMER $((SOLSTICE_SUMMER - EQUINOX_SPRING + 1)) $cday
	elif [ $today -lt $EQUINOX_AUTUMN ]; then # In the summer
		cday=$((today - SOLSTICE_SUMMER))
		compute_color $COLOR_SUMMER $COLOR_AUTUMN $((EQUINOX_AUTUMN - SOLSTICE_SUMMER + 1)) $cday
	elif [ $today -lt $SOLSTICE_WINTER ]; then # In the autumn
		cday=$((today - EQUINOX_AUTUMN))
		compute_color $COLOR_AUTUMN $COLOR_WINTER $((SOLSTICE_WINTER - EQUINOX_AUTUMN + 1)) $cday
	else
		cday=$((today - SOLSTICE_WINTER))
		compute_color $COLOR_WINTER $COLOR_SPRING $((NEW_YEARS_EVE - SOLSTICE_WINTER + 1 + EQUINOX_SPRING_NYEAR)) $cday
	fi
}
 
#************************ Tests if xrandr and imagemagick are installed ************************ 
if [ "$(whereis convert)" == "convert:" ]; then
	echo "Error : you must install convert (imagemagick)"
	exit 1
fi

#************************ Wallpaper designing  ************************ 
initialize

compute_todays_color

convert xc:black xc:black xc:black xc:$COLOR_DSKY xc:$todays_color \
	+append -filter Cubic -resize ${height}x${width}!\
	-rotate 90 -virtual-pixel transparent \
	-distort Barrel '0.0 0.0 -0.29 1.1' "$wallpaper" # "$wallpaper_tmp"   with text

#tmp=$(echo "$psize_date*($width/$ref_psize)" | bc -l)
#pointsize="${tmp%\.*}"
#text_date=$(date "+%e %B %Y")
#p_width=$((width/100*pp_date))
#p_height=$((height/100*pp_hdate))

#convert -size ${p_width}x${p_height} -background transparent -gravity center \
#	-pointsize $pointsize -strokewidth 1 -stroke $todays_color \
#	-fill $todays_color -font english label:"$text_date" "$tmp_text"
#convert $wallpaper_tmp $tmp_text -geometry +$((width-p_width))+0 -composite $wallpaper

#rm $wallpaper_tmp 2>/dev/null
#rm $tmp_text 2>/dev/null
#rm $tmp_gradient 2>/dev/null
